scripts = document.getElementsByTagName("script"); 
for(i = 0; i < scripts.length; i++){
    if(scripts[i].src.includes("errors.js")){
        script = scripts[i].src; 
        break; 
    }; 
}; 
root = script.substr(0, script.lastIndexOf("static/js/errors.js")); 
//root = "file:///E:/bootstrap/"; 
document.write(`<meta charset="utf-8">
<script src="${root}static/js/jquery.min.js"></script>
<script src="${root}static/bootstrap/js/bootstrap.bundle.min.js"></script>
<link href="${root}static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!--<script src="${root}static/clipboard/dist/clipboard.min.js"></script>-->
<link href="${root}static/bootstrap-icons/font/bootstrap-icons.min.css" rel="stylesheet">
<div class="modal fade" id="errors_shower" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <h1 class="modal-title fs-5">
                <i class="bi bi-x-circle"></i>
                错误
            </h1>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body" id="errors">
            <hr>
        </div>
        <div class="modal-footer">
            <button class="btn btn-outline-secondary" data-bs-dismiss="modal"><i class="bi bi-x-lg"></i>关闭</button>
            <!--<button class="btn btn-outline-primary" id="errors_copyer">复制</button>-->
            <button class="btn btn-outline-danger" onclick="throw_errors()"><i class="bi bi-bug"></i>重新抛出</button>
        </div>
    </div>
</div>
</div>`); 
errors = []; 
window.onerror = (message, source, lineno, colno, error) => {
    errors.push({
        "message": message, 
        "source": source, 
        "lineno": lineno, 
        "colno": colno, 
        "error": error
    }); 
    document.getElementById("errors").innerHTML += `<span>消息：${message}<br>文件：${source}<br>行号：${lineno}<br>列号：${colno}</span><hr>`; 
    return true; 
};
//errors = [{"message": "1", "source": "2", "lineno": "3", "colno": "4", "error": "5"}]; 

function throw_errors(){
    errors.forEach((item) => {
        console.log(item["error"]); 
    }); 
}; 

window.onload = function(){
    window.onerror = null; 
    //clipboard = new ClipboardJS(".btn")
    //document.getElementById("errors_copyer").setAttribute("data-clipboard-text", document.getElementById("errors").innerText); 
    if (errors.length != 0){
        $("#errors_shower").modal("show"); 
    }; 
}; 